package com.mass.waybill.order.service.impl;

import com.mass.core.utils.AjaxResponse;
import com.mass.core.utils.DateUtils;
import com.mass.waybill.order.dao.WbShippingOrderMapper;
import com.mass.waybill.order.model.WbShippingOrder;
import com.mass.waybill.order.model.WbShippingOrderExample;
import com.mass.waybill.order.service.WbShippingOrderService;
import com.mass.waybill.records.dao.WbSterilisationRecordsMapper;
import com.mass.waybill.records.model.WbSterilisationRecords;
import com.mass.waybill.records.model.WbSterilisationRecordsExample;
import com.mass.waybill.records.service.WbSterilisationRecordsService;
import com.mass.waybill.scheduleTypes.model.WbScheduleTypes;
import com.mass.waybill.scheduleTypes.service.WbScheduleTypesService;
import freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Author: ZSX
 * @Description: 订单service 接口实现类
 */
@Service
@Transactional
public class WbShippingOrderServiceImpl implements WbShippingOrderService {

    @Autowired
    private WbShippingOrderMapper wbShippingOrderMapper;

    @Autowired
    private WbScheduleTypesService wbScheduleTypesService;   //注入费用明细接口

    @Autowired
    private WbSterilisationRecordsMapper wbSterilisationRecordsMapper;

    @Autowired
    private WbSterilisationRecordsService wbSterilisationRecordsService;

    @Transactional
    @Override
    public int insert(WbShippingOrder wbShippingOrder) {
        wbShippingOrder.setDelState(0);//正常数据
        wbShippingOrder.setCreateTime(DateUtils.getDate());
        wbShippingOrder.setPlaceOrderTime(DateUtils.getDate("yyyy年MM月dd日 HH:mm"));//下单时间
        int number = wbShippingOrderMapper.insert(wbShippingOrder);
        if (number > 0) {
            wbShippingOrder.setOrderCode("ON" + StringUtil.leftPad(wbShippingOrder.getId().toString(), 6, "0"));
            //修改数据
            wbShippingOrderMapper.updateByPrimaryKeySelective(wbShippingOrder);
            List<WbScheduleTypes> wbScheduleTypesList = wbShippingOrder.getWbScheduleTypesList();
            if (!ObjectUtils.isEmpty(wbScheduleTypesList) && wbScheduleTypesList.size() > 0) {
                for (WbScheduleTypes wbScheduleTypes : wbScheduleTypesList) {
                    wbScheduleTypes.setStates(0);
                    wbScheduleTypes.setOrderCode(wbShippingOrder.getOrderCode());
                }
                //添加费用明细
                wbScheduleTypesService.replaceData(wbScheduleTypesList, 0, wbShippingOrder.getOrderCode(), null);
            }
        }
        return number;
    }

    @Transactional
    @Override
    public int delete(Long wbShippingOrderId) {
        WbShippingOrder wbShippingOrder = wbShippingOrderMapper.selectByPrimaryKey(wbShippingOrderId);
        wbShippingOrder.setDelState(1);
        if (wbShippingOrder.getStates() != null && (wbShippingOrder.getStates() == 0 || wbShippingOrder.getStates() == -1)) {
            return wbShippingOrderMapper.updateByPrimaryKeySelective(wbShippingOrder);
        } else {
            return -1;
        }
    }

    @Transactional
    @Override
    public int update(WbShippingOrder wbShippingOrder) {
        WbShippingOrder order = wbShippingOrderMapper.selectByPrimaryKey(wbShippingOrder.getId());
        if (order.getStates() != null && (order.getStates() == 0 || order.getStates() == -1)) {
            int number = wbShippingOrderMapper.updateByPrimaryKeySelective(wbShippingOrder);
            if (number > 0) {
                List<WbScheduleTypes> wbScheduleTypesList = wbShippingOrder.getWbScheduleTypesList();
                if (!ObjectUtils.isEmpty(wbScheduleTypesList) && wbScheduleTypesList.size() > 0) {
                    for (WbScheduleTypes wbScheduleTypes : wbScheduleTypesList) {
                        wbScheduleTypes.setStates(0);
                        wbScheduleTypes.setOrderCode(wbShippingOrder.getOrderCode());
                    }
                    //添加费用明细
                    wbScheduleTypesService.replaceData(wbScheduleTypesList, 0, wbShippingOrder.getOrderCode(), null);
                }
            }
            return number;
        } else {
            return -1;
        }
    }

    @Override
    @Transactional
    public int updateWbShippingOrder(WbShippingOrder wbShippingOrder) {
        if (!ObjectUtils.isEmpty(wbShippingOrder.getStates()) && wbShippingOrder.getStates() == 0) {//取消确认
            WbShippingOrder order = wbShippingOrderMapper.selectByPrimaryKey(wbShippingOrder.getId());
            //验证是否核销
            WbSterilisationRecordsExample example = new WbSterilisationRecordsExample();
            example.createCriteria().andOrderCodeEqualTo(order.getOrderCode().trim());
            List<WbSterilisationRecords> wbSterilisationRecordsList = wbSterilisationRecordsMapper.selectByExample(example);
            if (wbSterilisationRecordsList.size() > 0) {
                return -1;
            } else {
                return wbShippingOrderMapper.updateByPrimaryKeySelective(wbShippingOrder);
            }
        } else {
            if (!ObjectUtils.isEmpty(wbShippingOrder.getStandby()) && "0".equals(wbShippingOrder.getStandby())) {
                WbShippingOrder order = wbShippingOrderMapper.selectByPrimaryKey(wbShippingOrder.getId());
                if ("0".equals(order.getStandby().trim())) {
                    return -1;
                } else {
                    return wbShippingOrderMapper.updateByPrimaryKeySelective(wbShippingOrder);
                }
            }
            return wbShippingOrderMapper.updateByPrimaryKeySelective(wbShippingOrder);
        }
    }

    @Override
    public WbShippingOrder selectById(Long wbShippingOrderId) {
        WbShippingOrder wbShippingOrder = wbShippingOrderMapper.selectByPrimaryKey(wbShippingOrderId);
        //创建一个费用明细
        WbScheduleTypes wbScheduleTypes = new WbScheduleTypes();
        wbScheduleTypes.setOrderCode(wbShippingOrder.getOrderCode());
        wbScheduleTypes.setStates(0);
        List<WbScheduleTypes> wbScheduleTypesList = wbScheduleTypesService.findConditions(wbScheduleTypes);
        wbShippingOrder.setWbScheduleTypesList(wbScheduleTypesList);
        return wbShippingOrder;
    }

    @Override
    public WbShippingOrder selectByOrderCode(String orderCode) {
        WbShippingOrderExample example = new WbShippingOrderExample();
        example.createCriteria().andOrderCodeEqualTo(orderCode.trim()).andDelStateEqualTo(0);
        List<WbShippingOrder> list = wbShippingOrderMapper.selectByExample(example);
        WbShippingOrder wbShippingOrder = null;
        if (list.size() > 0) {
            wbShippingOrder = list.get(0);
            //创建一个费用明细
            WbScheduleTypes wbScheduleTypes = new WbScheduleTypes();
            wbScheduleTypes.setOrderCode(wbShippingOrder.getOrderCode());
            wbScheduleTypes.setStates(0);
            List<WbScheduleTypes> wbScheduleTypesList = wbScheduleTypesService.findConditions(wbScheduleTypes);
            wbShippingOrder.setWbScheduleTypesList(wbScheduleTypesList);
        }
        return wbShippingOrder;
    }

    @Override
    public AjaxResponse selectPage(WbShippingOrder wbShippingOrder, Integer page, Integer limit) {
        WbShippingOrderExample example = new WbShippingOrderExample();
        if (!ObjectUtils.isEmpty(page)) {
            example.setPageIndex((page - 1) * limit);
            example.setPageSize(limit);
        }
        example.setOrderByClause(" order_date ");
        WbShippingOrderExample.Criteria criteria = example.createCriteria();
        criteria.andDelStateEqualTo(0);
        if (!ObjectUtils.isEmpty(wbShippingOrder)) {
            //下单日期
            if (!ObjectUtils.isEmpty(wbShippingOrder.getOrderDate())) {
                criteria.andOrderDateBetween(wbShippingOrder.getOrderDate().split("~")[0].trim(), wbShippingOrder.getOrderDate().split("~")[1].trim());
            }
            //发运日期
            if (!ObjectUtils.isEmpty(wbShippingOrder.getDateShipment())) {
                criteria.andDateShipmentBetween(wbShippingOrder.getDateShipment().split("~")[0].trim(), wbShippingOrder.getDateShipment().split("~")[1].trim());
            }
            //货票号goodsWere
            if (!ObjectUtils.isEmpty(wbShippingOrder.getGoodsWere())) {
                criteria.andGoodsWereLike("%" + wbShippingOrder.getGoodsWere().trim() + "%");
            }
            //业务员
            if (!ObjectUtils.isEmpty(wbShippingOrder.getSalesman())) {
                criteria.andSalesmanEqualTo(wbShippingOrder.getSalesman().trim());
            }
            //业务类型
            if (!ObjectUtils.isEmpty(wbShippingOrder.getBusinessTypes())) {
                criteria.andBusinessTypesEqualTo(wbShippingOrder.getBusinessTypes().trim());
            }
            //品名
            if (!ObjectUtils.isEmpty(wbShippingOrder.getOfTheGoods())) {
                criteria.andOfTheGoodsEqualTo(wbShippingOrder.getOfTheGoods().trim());
            }
            //箱型
            if (!ObjectUtils.isEmpty(wbShippingOrder.getaBox())) {
                criteria.andABoxEqualTo(wbShippingOrder.getaBox().trim());
            }
            //箱号
            if (!ObjectUtils.isEmpty(wbShippingOrder.getCartonNo())) {
                criteria.andCartonNoLike("%" + wbShippingOrder.getCartonNo().trim() + "%");
            }
            //发货单位
            if (!ObjectUtils.isEmpty(wbShippingOrder.getDeliveryUnit())) {
                criteria.andDeliveryUnitEqualTo(wbShippingOrder.getDeliveryUnit());
            }
            //发出站
            if (!ObjectUtils.isEmpty(wbShippingOrder.getaStation())) {
                criteria.andAStationEqualTo(wbShippingOrder.getaStation().trim());
            }
            //到站
            if (!ObjectUtils.isEmpty(wbShippingOrder.getArriveStation())) {
                criteria.andArriveStationEqualTo(wbShippingOrder.getArriveStation().trim());
            }
            //收货单位
            if (!ObjectUtils.isEmpty(wbShippingOrder.getReceivingUnit())) {
                criteria.andReceivingUnitLike("%" + wbShippingOrder.getReceivingUnit().trim() + "%");
            }
            //结算人
            if (!ObjectUtils.isEmpty(wbShippingOrder.getSettlementPeople())) {
                criteria.andSettlementPeopleEqualTo(wbShippingOrder.getSettlementPeople());
            }
            //是否自运输
            if (!ObjectUtils.isEmpty(wbShippingOrder.getWhetherSelfTransport())) {
                criteria.andWhetherSelfTransportEqualTo(wbShippingOrder.getWhetherSelfTransport());
            }
            //司机
            if (!ObjectUtils.isEmpty(wbShippingOrder.getDriver())) {
                criteria.andDriverEqualTo(wbShippingOrder.getDriver().trim());
            }
            //车牌
            if (!ObjectUtils.isEmpty(wbShippingOrder.getLicensePlate())) {
                criteria.andLicensePlateEqualTo(wbShippingOrder.getLicensePlate().trim());
            }
            //是否需要发票
            if (!ObjectUtils.isEmpty(wbShippingOrder.getIsInvoice())) {
                criteria.andIsInvoiceEqualTo(wbShippingOrder.getIsInvoice());
            }
            //standby1  装货地点
            if (!ObjectUtils.isEmpty(wbShippingOrder.getStandby1())) {
                criteria.andStandby1EqualTo(wbShippingOrder.getStandby1().trim());
            }
            //standby2  运输方式
            if (!ObjectUtils.isEmpty(wbShippingOrder.getStandby2())) {
                criteria.andStandby2EqualTo(wbShippingOrder.getStandby2().trim());
            }
            //状态
            if (!ObjectUtils.isEmpty(wbShippingOrder.getStates())) {
                if (wbShippingOrder.getStates() == -2) {
                    criteria.andStatesGreaterThanOrEqualTo(0);
                    if(!ObjectUtils.isEmpty(wbShippingOrder.getWaybillStates())){
                        criteria.andStatesEqualTo(wbShippingOrder.getWaybillStates());
                    }
                } else {
                    criteria.andStatesEqualTo(wbShippingOrder.getStates());
                }
            }
        }
        //sterilisationAmount
        List<WbShippingOrder> list = wbShippingOrderMapper.selectByExample(example);
        for(WbShippingOrder wbShippingOrder1:list){
            wbShippingOrder1.setSterilisationAmount(wbSterilisationRecordsService.selectByOrderCode(wbShippingOrder1.getOrderCode()));
        }
        Long count = wbShippingOrderMapper.countByExample(example);
        return AjaxResponse.success("ok", list, count, page);
    }

    /**
     * 已冲销金额
     *
     * @param totalAmount 已冲销总金额
     * @param orderCode   订单编号
     * @return
     */
    @Override
    @Transactional
    public int sterilisation(String totalAmount, String orderCode) {
        if (!ObjectUtils.isEmpty(totalAmount) && !ObjectUtils.isEmpty(orderCode)) {
            WbShippingOrderExample example = new WbShippingOrderExample();
            WbShippingOrderExample.Criteria criteria = example.createCriteria();
            criteria.andAccountsReceivableAmountEqualTo(totalAmount.trim()).andOrderCodeEqualTo(orderCode.trim());
            WbShippingOrder wbShippingOrder = new WbShippingOrder();
            wbShippingOrder.setStates(3);
            return wbShippingOrderMapper.updateByExampleSelective(wbShippingOrder, example);
        }
        return 0;
    }
}
